package com.ydy.user.controller;

import com.ydy.common.BaseController;
import com.ydy.common.constant.Constant;
import com.ydy.common.model.Dict;
import com.ydy.common.model.Menu;
import com.ydy.common.model.User;
import com.ydy.common.util.JsonUtil;
import com.ydy.common.util.JwtTokenUtil;
import com.ydy.user.service.UserService;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class UserController extends BaseController {

    private static Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @Title: login
     * @author: dy.yin 2021/3/23 14:54
     * @param: [user, request]
     * @return: java.lang.Object
     * @throws
     */
    @PostMapping("/login")
    public Object login(@RequestBody User user, HttpServletRequest request) {
        if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getPassWord())) {
            return "请输入用户名和密码！";
        }
        //用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                user.getUserName(),
                user.getPassWord()
        );
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
        } catch (UnknownAccountException e) {
            System.out.println("用户名不存在！"+ e.getLocalizedMessage());
            throw new AuthenticationException("用户名不存在！");
        } catch (AuthenticationException e) {
            System.out.println("账号或密码错误！"+ e.getLocalizedMessage());
            throw new AuthenticationException("账号或密码错误！");
        } catch (AuthorizationException e) {
            System.out.println("没有权限！"+ e.getLocalizedMessage());
            throw new AuthenticationException("没有权限！");
        }

        user = userService.getUserByName(user.getUserName());
        String token = JwtTokenUtil.creatToken(user);
        //把登录信息存入redis
        userService.setUserInfoToRedis(user,token);

        Map<String,Object> data = new HashedMap<>();
        data.put("token",token);
        data.put("user",user);
        data.put("webSocketUrl",userService.getWebSocketUrl());

        return retResultVo(data);
    }

    /**
     * 查询用户菜单
     * @Title: queryUserMenus
     * @author: dy.yin 2021/3/23 14:56
     * @param: [userName]
     * @return: java.lang.Object
     * @throws
     */
    @GetMapping("/queryUserMenus")
    public Object queryUserMenus(String userCode){
        List<Menu> menuList = userService.queryUserMenus(userCode);
        return retResultVo(menuList);
    }

    /**
     * 重新加载缓存
     * @return
     */
    @GetMapping("/reLoadRedisCaChe")
    public Object reLoadRedisCaChe(){
        userService.reLoadRedisCaChe();
        return retResultVo();
    }
}
